import 'dart:async';
import 'dart:io' show Platform;

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart'
    show BMFMapSDK, BMF_COORD_TYPE;
import 'package:flutter_bmflocation/bdmap_location_flutter_plugin.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_oldman_health/navigator/tab_navigator.dart';
import 'package:flutter_oldman_health/pages/login_page.dart';
import 'package:flutter_oldman_health/utils/utils.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  Future<String> userIdFuture = Utils.getStrSharePreferences("userId");
  if (userIdFuture != null) {
    userIdFuture.then((value) {
      MyApp.userId = value;
      SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
          .then((_) {
        runApp(MyApp());
      });
    });
  } else {
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
        .then((_) {
      runApp(MyApp());
    });
  }
  LocationFlutterPlugin.setApiKey("xolHL3nn4nECYeE9W2HLFx0Kaito4ae6")
      .then((value) => print);

  if (Platform.isIOS) {
    BMFMapSDK.setApiKeyAndCoordType(
        'xolHL3nn4nECYeE9W2HLFx0Kaito4ae6', BMF_COORD_TYPE.BD09LL);
  } else if (Platform.isAndroid) {
    // Android 目前不支持接口设置Apikey,
    // 请在主工程的Manifest文件里设置，详细配置方法请参考官网(https://lbsyun.baidu.com/)demo
    BMFMapSDK.setCoordType(BMF_COORD_TYPE.BD09LL);
  }
}

class MyApp extends StatelessWidget {
  static String userId;

  @override
  Widget build(BuildContext context) {
    print('userId2: $userId');
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      locale: Locale('zh', 'CN'),
      localizationsDelegates: [
        GlobalCupertinoLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: [
        Locale('en', 'US'),
        Locale('zh', 'CN'),
      ],
      // home: hasLogin ? TabNavigation() : LoginPage(),
      builder: EasyLoading.init(),
      home: userId != null ? TabNavigation() : LoginPage(),
      // home: userId != null ? TabNavigation() : LoginPage(),
    );
  }
}
